package com.ludomancy.sos64.components;

import java.awt.Graphics;
import java.awt.Point;
import java.util.Vector;

import com.ludomancy.gamelib.ImageAnimation;
import com.ludomancy.gamelib.ImageStrip;
import com.ludomancy.gamelib.Randomizer;
import com.ludomancy.gamelib.ResourceManager;
import com.ludomancy.gamelib.Sound;
import com.ludomancy.sos64.Accumulator;
import com.ludomancy.sos64.Component;
import com.ludomancy.sos64.Game;

public class Larva64 extends Component
{
	private ImageAnimation larva;
	private boolean eatenFood = false;
	private Accumulator stir;
	
	public Larva64(Point pos)
	{
		super(pos.x,pos.y);
		if(larva == null)
		{
			larva = new ImageAnimation(new ImageStrip(ResourceManager.getImage("larva.png"), 23, 19), 4);
		}
		stir = new Accumulator(Randomizer.getRandom(1500,3000));
		larva.setSequenceBouncingLoop();
	}
	
	public Point getSize()
	{
		return new Point(23,19);
	}

	public void paint(Graphics g, int px, int py)
	{
		larva.paintCurrentFrame(g, px, py);
	}
	
	public boolean isOrganic()
	{
		return true;
	}

	public void update()
	{
		stir.update();
		larva.update();
		if(stir.isDone())
		{
			larva.rewind();
			larva.play();
			stir.reset(Randomizer.getRandom(1000,4000));
		}
		Dam b = null;
		Vector v = Game.game.getComponents(Dam.class);
		if(!v.isEmpty())
		{
			b = (Dam) v.elementAt(0);
		}
		if(b != null && b.area().contains(getHotspot()))
		{
			b.consume();
			if(larva.isOver())
				larva.rewind();
			larva.play();
		}
		
		if(eatenFood && larva.isOver())
		{
			// grow!
			Game.game.components.remove(this);
			Game.game.add(new Teen64(new Point(x-11,y-6)));
		}
	}

	public void feed()
	{
		Sound.play("grow.wav");
		eatenFood = true;
		larva.play();
		larva.rewind();
	}
}
